// 编码 
// 封装 getCookie(key)   return value  
// decodeURIComponent 解码 
export function getCookie(key) {
    var cookie = document.cookie
    if (cookie) {
        var arr = cookie.split("; ")
        // 循环一个数组然后  从数组里面取出对应的 key-value 
        for (var i = 0; i < arr.length; i++) {
            var item = arr[i]
            var attr = item.split("=")[0]
            var value = item.split("=")[1]    // 修改外部环境的 value 的值 
            if (key == attr) {
                return decodeURIComponent(value)
            }
        }
    }
    return ""
}

// 设置一个cookie path='/' 默认  值为  /  
// encodeURIComponent 编码 
export function setCookie(key, val, day, path = '/') {
    if (day) {
        var date = new Date()
        date.setDate(date.getDate() + day)
        document.cookie = `${key}=${encodeURIComponent(val)};expires=${date.toUTCString()};path=${path}`

    } else {
        document.cookie = key + "=" + encodeURIComponent(val) + ";path=/";
    }
}

// 通过key 删除 
export function delCookie(key) {
    setCookie(key, "", -1)
}